#include "myglwidget.h"
#include <cml/cml.h>

static const float POINT_PART = 0.8f;

MyGLWidget::MyGLWidget(const QGLFormat &fmt, QWidget *parent, MainWindow &mainWindow)
: QGLWidget(fmt, parent),
  m_vectorX(1.0f),
  m_vectorY(1.0f),
  m_vectorZ(1.0f),
  m_mtxStack(*this, mainWindow)
{
	setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}


void MyGLWidget::initializeGL()
{
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	//glEnable(GL_DEPTH_TEST);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

void MyGLWidget::resizeGL(int newW, int newH)
{
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	gluPerspective(30.0f, 1.0f * newW / newH, 2.0f, 100.0f);

	glMatrixMode(GL_MODELVIEW);
	glViewport(0, 0, newW, newH);
}

void MyGLWidget::paintGL()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(15.0f, 15.0f, 15.0f,
			   0.0f,  0.0f,  0.0f,
			   0.0f,  1.0f,  0.0f);

	drawAxis(5.0f);
	
	m_mtxStack.applyStack();
	drawVector();
	m_mtxStack.popStack();
}

void MyGLWidget::drawAxis(float length/* = 1.0f*/, float markSize/* = 0.1f*/)
{
	glBegin(GL_LINES);

	glColor3f(1.0f, 0.0f, 0.0f);
	glVertex3f(0.0f, 0.0f, 0.0f);
	glVertex3f(length, 0.0f, 0.0f);
	
	for ( int i = 1; i < (int)length; ++i )
	{
		glVertex3f(1.0 * i, 0.0f, 0.0f);
		glVertex3f(1.0 * i, markSize, 0.0f);

		glVertex3f(1.0 * i, 0.0f, 0.0f);
		glVertex3f(1.0 * i, 0.0f, markSize);
	}

	glColor3f(0.0f, 1.0f, 0.0f);
	glVertex3f(0.0f, 0.0f, 0.0f);
	glVertex3f(0.0f, length, 0.0f);

	for ( int i = 1; i < (int)length; ++i )
	{
		glVertex3f(0.0f, 1.0 * i, 0.0f);
		glVertex3f(markSize, 1.0 * i, 0.0f);

		glVertex3f(0.0f, 1.0 * i, 0.0f);
		glVertex3f(0.0f, 1.0 * i, markSize);
	}


	glColor3f(0.0f, 0.0f, 1.0f);
	glVertex3f(0.0f, 0.0f, 0.0f);
	glVertex3f(0.0f, 0.0f, length);

	for ( int i = 1; i < (int)length; ++i )
	{
		glVertex3f(0.0f, 0.0f, 1.0 * i);
		glVertex3f(markSize, 0.0f, 1.0 * i);

		glVertex3f(0.0f, 0.0f, 1.0 * i);
		glVertex3f(0.0f, markSize, 1.0 * i);
	}

	glEnd();
}

void MyGLWidget::drawVector()
{
	drawAxis();
	
	float pointX = m_vectorX * POINT_PART;
	float pointY = m_vectorY * POINT_PART;
	float pointZ = m_vectorZ * POINT_PART;
	
	glBegin(GL_LINES);
	
	glColor3f(0.5f, 0.5f, 0.5f);
	glVertex3f(0.0f, 0.0f, 0.0f);
	glVertex3f(pointX, pointY, pointZ);
	
	glColor3f(1.0f, 1.0f, 1.0f);
	glVertex3f(pointX, pointY, pointZ);
	glVertex3f(m_vectorX, m_vectorY, m_vectorZ);
	
	////////////////////////////
	glColor3f(0.5f, 0.5f, 0.0f);
	glVertex3f(0.0f, 0.0f, 0.0f);
	glVertex3f(pointX, pointY, 0.0f);
	
	glColor3f(1.0f, 1.0f, 0.0f);
	glVertex3f(pointX, pointY, 0.0f);
	glVertex3f(m_vectorX, m_vectorY, 0.0f);

	////////////////////////////
	glColor3f(0.0f, 0.5f, 0.5f);
	glVertex3f(0.0f, 0.0f, 0.0f);
	glVertex3f(0.0f, pointY, pointZ);
	
	glColor3f(0.0f, 1.0f, 1.0f);
	glVertex3f(0.0f, pointY, pointZ);
	glVertex3f(0.0f, m_vectorY, m_vectorZ);

	////////////////////////////
	glColor3f(0.5f, 0.0f, 0.5f);
	glVertex3f(0.0f, 0.0f, 0.0f);
	glVertex3f(pointX, 0.0f, pointZ);
	
	glColor3f(1.0f, 0.0f, 1.0f);
	glVertex3f(pointX, 0.0f, pointZ);
	glVertex3f(m_vectorX, 0.0f, m_vectorZ);

	glEnd();
	
	glBegin(GL_QUADS);
	
	glColor4f(1.0f, 1.0f, 0.0f, 0.1f);
	glVertex3f(0.0f, 0.0f, 0.0f);
	glVertex3f(m_vectorX, 0.0f, 0.0f);
	glVertex3f(m_vectorX, m_vectorY, 0.0f);
	glVertex3f(0.0f, m_vectorY, 0.0f);
	
	glColor4f(1.0f, 0.0f, 1.0f, 0.1f);
	glVertex3f(0.0f, 0.0f, 0.0f);
	glVertex3f(0.0f, 0.0f, m_vectorZ);
	glVertex3f(m_vectorX, 0.0f, m_vectorZ);
	glVertex3f(m_vectorX, 0.0f, 0.0f);

	glColor4f(0.0f, 1.0f, 1.0f, 0.1f);
	glVertex3f(0.0f, 0.0f, 0.0f);
	glVertex3f(0.0f, m_vectorY, 0.0f);
	glVertex3f(0.0f, m_vectorY, m_vectorZ);
	glVertex3f(0.0f, 0.0f, m_vectorZ);

	glEnd();
	
	glBegin(GL_LINES);

	glColor3f(0.5f, 0.0f, 0.0f);
	glVertex3f(m_vectorX, m_vectorY, m_vectorZ);
	glVertex3f(0.0f, m_vectorY, m_vectorZ);
	
	glColor3f(0.0f, 0.5f, 0.0f);
	glVertex3f(m_vectorX, m_vectorY, m_vectorZ);
	glVertex3f(m_vectorX, 0.0f, m_vectorZ);
	
	glColor3f(0.0f, 0.0f, 0.5f);
	glVertex3f(m_vectorX, m_vectorY, m_vectorZ);
	glVertex3f(m_vectorX, m_vectorY, 0.0f);
	
	glEnd();
}

void MyGLWidget::setVector(float x, float y, float z)
{
	m_vectorX = x;
	m_vectorY = y;
	m_vectorZ = z;
}
